package org.ekstrabilet.admin;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class UserTable extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] columnNames; 
	
	private List<Object[]> data;
	
	public UserTable() {
		columnNames = new String[] { "No", "Login", "Name", "Surname", "Type"};
		data = new ArrayList<Object[]>();
	}
	
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		return data.size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		return data.get(row)[col];
	}

	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}
	
	public boolean addRow(Row row) {
		Object[] add = row.getObject();
		Iterator<Object[]> it = data.iterator();
		while(it.hasNext()) {
			Object[] old = it.next();
			if(old[1].equals(add[1])) {
				return false;
			}
		}
		add[0] = getRowCount();
		data.add(add);
		return true;
	}

	public boolean editRow(Row row) {
		Object[] edit = row.getObject();
		Iterator<Object[]> it = data.iterator();
		while(it.hasNext()) {
			Object[] old = it.next();
			if(old[1].equals(edit[1])) {
				edit[0] = old[0];
				Integer i = (Integer) old[0];
				data.set(i.intValue(), edit);
				return true;
			}
		}
		return false;
	}
	
	public boolean delRow(Row row) {
		Object[] del = row.getObject();
		Iterator<Object[]> it = data.iterator();
		while(it.hasNext()) {
			Object[] old = it.next();
			if(old[1].equals(del[1])) {
				Integer i = (Integer) old[0];
				data.remove(i.intValue());
				return true;
			}
		}
		return false;
	}
	
	public void clear() {
		data.clear();
	}
}
